package cn.signalpha.modules.shop.wrapper;

import cn.signalpha.common.constant.MsgConstant;
import cn.signalpha.core.mp.support.BaseEntityWrapper;
import cn.signalpha.core.tool.utils.BeanUtil;
import cn.signalpha.core.tool.utils.ObjectUtil;
import cn.signalpha.core.tool.utils.SpringUtil;
import cn.signalpha.modules.shop.entity.Coupon;
import cn.signalpha.modules.shop.entity.Seller;
import cn.signalpha.modules.shop.service.CouponUserService;
import cn.signalpha.modules.shop.service.SellerService;
import cn.signalpha.modules.shop.vo.CouponVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.List;
import java.util.Objects;

/**
 * 包装类,返回视图层所需的字段
 */
public class CouponWrapper extends BaseEntityWrapper<Coupon, CouponVO> {

	private static final SellerService sellerService;
	private static final CouponUserService couponUserService;

	static {
		sellerService = SpringUtil.getBean(SellerService.class);
		couponUserService = SpringUtil.getBean(CouponUserService.class);
	}

	public static CouponWrapper build() {
		return new CouponWrapper();
	}

	@Override
	public CouponVO entityVO(Coupon coupon) {
		CouponVO couponVO = Objects.requireNonNull(BeanUtil.copy(coupon, CouponVO.class), MsgConstant.OBJECT_NULL);
		if(ObjectUtil.isNotEmpty(couponVO.getSellerId()) && couponVO.getSellerId() > 0){
			Seller seller = sellerService.getBaseById(couponVO.getSellerId());
			if(ObjectUtil.isNotEmpty(seller)){
				couponVO.setSellerName(seller.getName());
				couponVO.setSellerLogo(seller.getLogo());
			}
		}
		return couponVO;
	}

	public IPage<CouponVO> pageVO(IPage<Coupon> pages, Long userId) {
		List<CouponVO> records = this.listVO(pages.getRecords());
		records.forEach(couponVO -> {
			couponVO.setIsGet(couponUserService.isGet(couponVO.getId(), userId));
		});
		IPage<CouponVO> pageVo = new Page(pages.getCurrent(), pages.getSize(), pages.getTotal());
		pageVo.setRecords(records);
		return pageVo;
	}

}
